package com.alalei.moodview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
@SuppressLint("ViewConstructor")
public class MoodMarkerView extends MarkerView {

    private static final String TAG = MoodMarkerView.class.getSimpleName();
    private final TextView tvContent;
    private int index;

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MoodMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);

        tvContent = findViewById(R.id.tvContent);
    }

    // runs every time the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        index = highlight.getDataSetIndex();//这个方法用于获得折线是哪根
        Log.d(TAG, "这个方法用于获得折线是哪根 >>>> " + index);

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

//            tvContent.setText(Utils.formatNumber(ce.getHigh(), 0, true));
            tvContent.setText(getMarkerInfo(ce.getHigh()));
        } else {

//            tvContent.setText(Utils.formatNumber(e.getY(), 0, true));
            tvContent.setText(getMarkerInfo(e.getY()));
        }

        super.refreshContent(e, highlight);
    }

    private String getMarkerInfo(float value) {
        String info;
        // 由于先加载折线--笑，再记载 哭
        if (0 == index) {
            info = "笑：" + Utils.formatNumber(value, 0, true);
        } else if (1 == index) {
            info = "哭：" + Utils.formatNumber(0 - value, 0, true);
        } else {
            info = Utils.formatNumber(value, 0, true);
        }
        return info;
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }

}
